package com.erb.config.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class IndexLoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();

		// 获取session中的对象
		Object user = session.getAttribute("user");
		if (user == null) {
			// 未登录，去往登录页
			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}

		return true;
	}
}
